var recensee_inline_widget = {};

recensee_inline_widget.g_id = function(e) {
    return document.getElementById(e)
}

recensee_inline_widget.has_class = function(e, c) {
    var eC = e.className;
    return (eC.length > 0 && (eC == c ||
        new RegExp("(^|\\s)" + c + "(\\s|$)").test(eC)));
};

recensee_inline_widget.add_class = function(e, c) {
  if (!this.hasClass(e, c)) {
      e.className += ' '+c;
  }
}

recensee_inline_widget.remove_class = function(e, c) {
    e.className = e.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), '');
};

recensee_inline_widget.get_cookie = function(cn) {
    var c=document.cookie;
    if (c.length>0) {
        var cs=c.indexOf(cn + "=");
        if (cs!=-1) {
            cs=cs + cn.length+1;
            var ce=c.indexOf(";",cs);
            if (ce==-1) ce=c.length;
            return c.substring(cs,ce);
        }
    }
    return false;
}

recensee_inline_widget.init = function(options)
{
    document.write('<div id="recensee-inline-widget-body"></div>');
    
    options.shop = parseInt(options.shop);
    if (options.shop == 0) {
        return false;
    }
    recensee_inline_widget.shop = options.shop;
    
    if (options.product == 0 || options.product == '') {
        return false;
    }
    recensee_inline_widget.product = options.product;
    
    if (options.width) {
        recensee_inline_widget.width = options.width;
    } else {
        recensee_inline_widget.width = '100%';
    }
    recensee_inline_widget.proto   = (document.location.protocol == 'https:') ? 'https' : 'http';
    if (options.testmode) {
        recensee_inline_widget.host = (options.testmode == 1) ? 'recensee.localhost' : 'test.recensee.com';
    } else {
        recensee_inline_widget.host = 'recensee.com';
    }

    var wrap = document.createElement('div');
    wrap.id = 'recensee-inline-widget';
    wrap.style.display = 'block';
    wrap.innerHTML = '<iframe id="recensee-inline-iframe" allowtransparency="true" src="" scrolling="no" frameborder="0" style="width:' + this.width + '; height:430px; background-color: transparent;"></iframe>\n';
    
    var body = document.getElementById('recensee-inline-widget-body');
    body.appendChild(wrap);

    if (window.addEventListener) {      // all browsers except IE before version 9
        window.addEventListener("message", recensee_inline_widget.receive_message, false);
    } else if (window.attachEvent) {    // IE before version 9
        window.attachEvent("onmessage", recensee_inline_widget.receive_message);
    }
}

recensee_inline_widget.show = function()
{
    var src = recensee_inline_widget.proto + '://' + recensee_inline_widget.host +
            '/widget/id' + recensee_inline_widget.shop + '/index/index/' + this.product;
    this.g_id('recensee-inline-iframe').src = src;
}

/*recensee_inline_widget.get_count = function()
{   
    return recensee_inline_widget.count; 
}

recensee_inline_widget.init_count = function()
{
    var script = document.createElement('script');
    script.src     = recensee_inline_widget.proto + '://' + recensee_inline_widget.host +
            '/widget/id' + recensee_inline_widget.shop + '/index/count/' + this.product;
    script.type    = 'text/javascrip';
    script.charset = 'utf-8';  
    document.getElementsByTagName('head')[0].appendChild(script);   
}

recensee_inline_widget.set_count = function(count)
{
    recensee_inline_widget.count = count;  
}*/

recensee_inline_widget.receive_message = function(event)
{
    if (event.origin !== (recensee_inline_widget.proto + "://" + recensee_inline_widget.host)) {
        return;    
    }
    var data = event.data.split(' ', 2);
    if (data[0] != 'recensee') {
        return;   
    }
    var height = parseInt(data[1]);
    var scroll = false;
    if (data[2] && data[2] == 'scroll') {
        scroll = true;    
    }
    if (recensee_inline_widget.g_id('recensee-inline-iframe').height != height  + 'px') {
        recensee_inline_widget.g_id('recensee-inline-iframe').height = height + 'px';   
    }
    if (recensee_inline_widget.g_id('recensee-inline-iframe').style.height != height  + 'px') {
        recensee_inline_widget.g_id('recensee-inline-iframe').style.height = height + 'px';  
    }
    if (scroll) {
        window.scrollBy(0, -height);
    }
}